package client;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.Serializable;

import javax.swing.*;

public class Login extends JFrame implements ActionListener ,Serializable{
	
	private String user = new String();
	private String psw = new String();
	
	private JLabel userlabel;
	private JTextField userfield;
	private JLabel pswlabel;
	private JPasswordField pswfield;
		
	public Login(){
		super("Schermata iniziale");
		setExtendedState(MAXIMIZED_BOTH);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.CENTER;
		
		userlabel = new JLabel("   User Name");
		pswlabel = new JLabel("   Password");
		userlabel.setForeground(Color.WHITE);
		pswlabel.setForeground(Color.WHITE);
		
		userfield = new JTextField(15);
		pswfield = new JPasswordField(15);
		
		userfield.addActionListener(this);
		pswfield.addActionListener(this);
		
		userlabel.setPreferredSize(new Dimension(100,20));
		userfield.setPreferredSize(new Dimension(150,20));
		pswlabel.setPreferredSize(new Dimension(100,20));
		pswfield.setPreferredSize(new Dimension(150,20));
		
		JPanel pannello1 = new JPanel();
		pannello1.setLayout(new FlowLayout());
		pannello1.setOpaque(false);	
		
		pannello1.add(userlabel);
		pannello1.add(userfield);
		pannello1.add(pswlabel);
		pannello1.add(pswfield);
		
		PannelloSfondo pannello = new PannelloSfondo(new File("sfondo2.jpg"));
		pannello.setLayout(new GridBagLayout());
		
		pannello.add(pannello1, c);
		
		add(pannello, BorderLayout.CENTER);
		
		setVisible(true);
		
		synchronized(this){
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void actionPerformed(ActionEvent e) {
		synchronized(this){
			user = userfield.getText();
			psw = pswfield.getText();
			notifyAll();
			dispose();
			}
	}
	
	public String getUser(){
		return user;
	}
	
	public String getPsw(){
		return psw;
	}

}
